package QuizPackage;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.StringTokenizer;

import javax.servlet.http.HttpServletRequest;

public class FillInBlank extends Question {

	private String qPart1;
	private String qPart2;
	private String[] responses;
	private boolean casesensitive;
	
	/**
	 * Constructor.
	 * @param ID Unique ID identifying this Question.  Allows it to specify
	 * the parameter related to this Question in the HTML formatting.
	 * @param question Text that makes up the question.
	 * @param responses Valid responses to the question.  The first response
	 * is the "preferred" response.
	 */
	/*public FillInBlank(int ID,String question,String[] responses)
	{
		super(ID);
		this.question = question;
		this.responses = responses;
	}*/
	
	/**
	 * Constructor.
	 * @param ID Unique ID identifying this Question.  Allows it to specify
	 * the parameter related to this Question in the HTML formatting.
	 * @param rs A ResultSet containing the info of this question, as stored in
	 * the database.
	 */
	public FillInBlank(int ID,ResultSet rs)
	{
		super(ID);
		try {
			qPart1 = rs.getString("qpart1");
			qPart2 = rs.getString("qpart2");
			casesensitive = Integer.parseInt(rs.getString("casesensitive")) == 1;
			StringTokenizer responseTokens = new StringTokenizer(rs.getString("response"),"|");
			responses = new String[responseTokens.countTokens()];
			int i = 0;
			while(responseTokens.hasMoreTokens())
			{
				responses[i] = responseTokens.nextToken();
				i++;
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * Gets a string of HTML for this Question.  This HTML includes the
	 * Question text and the input for the Question (specific to
	 * this question ID).  Does NOT return a form.
	 */
	public String getHTML()
	{
		StringBuilder html = new StringBuilder();
		html.append("<p>"+qPart1);
		html.append("<input name=\""+questionID+"\" type=\"text\">");
		html.append(qPart2);
		html.append("</p>");
		String result = new String(html);
		return result;
	}
	
	/**
	 * Returns 1 point if params[0] is a correct answer to
	 * the question.  Otherwise returns 0 points.
	 * Method should be called as
	 * checkAnswer(request.getParameterValues(ID))
	 */
	public int checkAnswer(String[] params)
	{
		for(int i = 0 ; i < responses.length ; i++)
		{
			if(!casesensitive && params[0].compareToIgnoreCase(responses[i]) == 0) return 1;
			if(casesensitive && params[0].equals(responses[i])) return 1;

		}
		return 0;
	}
	
	/**
	 * Returns the total points possible for this question.
	 */
	public int totalPoints()
	{
		return 1;
	}
	
	/**
	 * Returns a String describing the correct answer for this question.
	 */
	public String correctAnswer()
	{
		return responses[0];
	}
	
	public String toString()
	{
		return qPart1+"__________"+qPart2;
	}
	
	/**
	 * Returns a String of HTML that describes the UI users
	 * should use to create this question.  Returns only input
	 * types and text...does NOT return a form.
	 */
	public static String createQHTML()
	{
		StringBuilder html = new StringBuilder();
		html.append("<p>Enter the first part of the question.</p>");
		html.append("<input name=\"question1\" type=\"text\">");
		html.append("<p>Enter the second part of the question.</p>");
		html.append("<input name=\"question2\" type=\"text\">");
		html.append("<p>Enter the preferred response.</p>");
		html.append("<input name=\"preferred\" type=\"text\">");
		html.append("<p>Enter up to 4 additional responses.</p>");
		html.append("<input name=\"r1\" type=\"text\">");
		html.append("<input name=\"r2\" type=\"text\">");
		html.append("<input name=\"r3\" type=\"text\">");
		html.append("<input name=\"r4\" type=\"text\">");
		html.append("<p>Enter additional responses.(Separate responses with only a | character)");
		html.append("<input name=\"extra\" type=\"text\">");
		html.append("<p>Should this question be case-sensitive?</p>");
		html.append("<input name=\"casesensitive\" type=\"radio\" value=\"false\" checked>No");
		html.append("<input name=\"casesensitive\" type=\"radio\" value=\"true\">Yes");
		
		String result = new String(html);
		return result;
	}
	
	/**
	 * Enters the question with the form data from request
	 * into the proper question database table.  Will read
	 * the same input as created in createQHTML.
	 */
	public static void submitQuestion(int quizid,HttpServletRequest request)
	{
		QuizDBC dbc = new QuizDBC();
		String question1 = request.getParameter("question1");
		String question2 = request.getParameter("question2");
		
		int casesensitive = (request.getParameter("casesensitive").equals("false")) ? 0 : 1;
		
		//System.out.println(question2);
		StringBuilder response = new StringBuilder();
		response.append(request.getParameter("preferred"));
		for(int i=1 ; i <= 4 ; i++)
		{
			if(request.getParameter("r"+i).length()>0) response.append("|"+request.getParameter("r"+i));
		}
		if(request.getParameter("extra").length()>0) response.append("|"+request.getParameter("extra"));
		dbc.editTable("INSERT INTO fillinblank VALUES(\""+question1+"\",\""+question2+"\",\""+response+"\","+quizid+","+casesensitive+");");
	}
}
